package com.example.node.message;

/*
 * @className: ResponseFuture
 * @author: chentao
 * @date: 2025/10/25 下午5:26
 * @Version: 1.0
 * @description:
 */
public class ResponseFuture {

    private String requestId;

    private Request request;

    private Response response;

    private volatile Object lock = new Object();

    private volatile boolean isDone = false;

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    public Object getLock() {
        return lock;
    }

    public void setLock(Object lock) {
        this.lock = lock;
    }

    public boolean isDone() {
        return isDone;
    }

    public void setDone(boolean done) {
        isDone = done;
    }

    public ResponseFuture(Request request) {
        this.requestId = request.getRequestId();
        this.request = request;
    }

    public void set(Response response) {
        synchronized (lock) {
            this.isDone = true;
            this.response = response;
            lock.notify();
        }
    }

    public Response get() throws Exception {
        synchronized (lock) {
            if (isDone) {
                return response;
            } else {
                lock.wait(request.getTimeout());
                return response;
            }
        }
    }
}
